<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Services\Admin\AdminService;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    protected $request;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * 用户注册
     * @param AdminService $adminService
     * @return array
     * @throws \Illuminate\Validation\ValidationException
     */
    public function register(AdminService $adminService){
        $this->validate($this->request, [
            'username' => 'required|unique:wt_admin',
            'email' => 'required|email',
            'password' => 'required|min:6|max:255'
        ]);

        $result = $adminService->register($this->request->post());
        return $result;
    }

    /**
     * 用户登录
     * @param AdminService $adminService
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function login(AdminService $adminService){
        $this->validate($this->request, [
            'username' => 'required',
            'password' => 'required|min:6|max:255'
        ]);

        $result = $adminService->login($this->request->post());
        return $result;
    }
}
